#include "pluginrack.h"
#include "free-cube/aplugin.h"
#include "free-cube/adata.h"
#include "free-cube/logger.h"

using namespace fc;
PluginRack::PluginRack(){
}

PluginRack::~PluginRack(){
    qDeleteAll(plugins_);
}

struct PluginRack::PluginData {
    PluginData(APlugin & p) : plugin(p) {
        plugin.set_data(data_out);
    }
    APlugin & plugin;
    fc::DataList data_out;
};

void PluginRack::load(APlugin &pl) {
    PluginData * pd = new PluginData(pl);
    plugins_.append(pd);
}

void PluginRack::process(size_t size) {
    foreach (PluginData * pd, plugins_){
        pd->data_out.clear();
        pd->plugin.process(size);
    }
}


void PluginRack::connect_data(int out_plugin, int in_plugin){
    if (plugins_.count() <= out_plugin || plugins_.count() <= in_plugin ||
            out_plugin < 0 || in_plugin < 0 ) return;

    fc::log("RACK : connecting " + plugins_[out_plugin]->plugin.name() + "->"+
            plugins_[in_plugin]->plugin.name());

    plugins_[in_plugin]->plugin.set_input_data(plugins_[out_plugin]->data_out);

    if ( out_plugin < in_plugin ) plugins_.move(out_plugin, in_plugin);
};
